package etnorservice.etnor_service.service.impl;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/11/30/11:25
 **/



import etnorservice.etnor_service.domain.dto.CommentDTO;
import etnorservice.etnor_service.domain.po.Comment;
import etnorservice.etnor_service.mapper.CommentMapper;
import etnorservice.etnor_service.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    @Transactional
    public CommentDTO createComment(CommentDTO comment) {
        comment.setLikeCount(0);
        commentMapper.insert(comment);
        return comment;
    }

    @Override
    public List<Comment> getCommentsByApiId(Long apiId) {
        return commentMapper.findByApiId(apiId);
    }

    @Override
    @Transactional
    public Comment updateComment(Comment comment) {
        comment.setUpdateTime(new Date());
        commentMapper.update(comment);
        return comment;
    }

    @Override
    @Transactional
    public void deleteComment(Long id) {
        commentMapper.deleteById(id);
    }

    @Override
    public Comment getCommentById(Long id) {
        return commentMapper.findById(id);
    }

    @Override
    @Transactional
    public void updateCommentLikes(Long id, int likesCount) {
        commentMapper.updateLikesCount(id, likesCount);
    }

    @Override
    @Transactional
    public void updateCommentReplies(Long id, int repliesCount) {
        commentMapper.updateRepliesCount(id, repliesCount);
    }
}